function [ EC ] = APEfullshebang( exwave, batchfullfolder, outtype, slicewave )

%[ EC ] = APEfullshebang(exwave, batchfullfolder, outtype, slicewave)
%
%   Process a SIF file completly and produces a PDF report in a
%   subdirectory of the data folder
%
% INPUT     exwave          : wavelenght of Raman laser. Use 0 for
%                               non-raman data.
%           batchfullfolder : [0] = process single file
%                              1  = process all files in folder
%           outtype         : sets the type of file to convert to. 
%                              1  or 'csv' =  comma seperated value (*.csv)
%                             [2] or 'tsv' =  TAB seperated value (*.asc)
%                              3  or 'ssv' =  SPACE seperated value (*.asc)
%                              4  or 'xls' =  Excel file (*.xls)
%           slicewave       : wavelenght at which to slice kinetic data.
%                               Use 0 for non-kinetic data. Multiple
%                               wavelength can be entered [300 420 500].
%
% OUTPUT    EC          :   error code (0 = success, 1 = error)
%
% EXAMPLE:  process all files in one folder with a determined Laser
%           wavelength (=> see RaCaTo) of 404.8nm and CSV output:
%              [!! All files in folder must be of Raman type!!]
%
%  > APEfullshebang(404.8, 1, 0, 1)


%  (c) F. Dworkowski, 2011
%      Version 1.1, 2011-12-15
%      Version 1.0, 2011-09-26

% DEPENDENCIES:     - uvvisslicemulti
%                   - loadsif_nonGUI
%                   - APEramanshift
%                   - singleALS
%                   - APEgenerateoverview
%                   - APEmat2asc
                
                


if nargin<1
    help APEfullshebang
    EC = 1;
    return
end

%% ===== CONFIG =====
subfoldername = 'APEout';
oldpath = cd;


%% ===== VALIDATION & AUTOCONFIG =====
if exist('batchfullfolder','var') == 0
    batchfullfolder = 0;
end
if exist('outtype','var')==0
    outtype = 2;
end

%% ===== CODE =====
% ------ choose file and change path -------
[FileName,PathName,~] = uigetfile({'*.sif';'*.*'});
if isequal(PathName,0)
   disp('User selected Cancel')
   EC = 1;
   return
else
   disp(['User selected ', fullfile(PathName, FileName)])
end

cd(PathName)

% ------ check for batch option ------
if batchfullfolder == 1
     filelist=dir('*.sif');
else
    filelist.name=FileName;
end

%% ------ START LOOP ------
% !!!!!!!!! all processing should go here !!!!!!!!!

for i = 1:length(filelist)  % Do for all detected files
    close all
    [~, datasetname, ~] = fileparts(filelist(i).name);
    % Convert to MAT
    loadsif_nonGUI(datasetname,1) 
    % check for Raman data
    if exwave ~= 0
        processed = 1;
        % Ramanshift calculation
        EC = APEramanshift(datasetname, exwave);
        % ALS baseline correction
        EC = singleALS(datasetname);
    else
        processed = 0;
    end
    

    %% Generate PDF report and TEXT in subfolder
    if exist(subfoldername,'dir') ~= 1
        mkdir(subfoldername)
    end
    cd(subfoldername)    
    fulldatasetname=fullfile(PathName,datasetname);
    % generate OVERVIEW
    APEgenerateoverview(fulldatasetname);
    % Do kinetic slicing
    if slicewave ~= 0
        uvvisslicemulti(fulldatasetname,1,1,slicewave,0)
            % ===== SAVE PDF ===== 
            [~, puredatasetname, ext] = fileparts(datasetname); 
            % save PDF
            orient tall
            print(541,'-dpdf','-painters',[puredatasetname,'_SLICES'])
    end
    % Export TEXT data
    APEmat2asc(fulldatasetname,outtype,processed);

    cd ..
        
%% END PROCESsING BLOCK
        

end %END LOOP

%% ===== Closing =====
cd(oldpath);
successbox = msgbox({'Full shebang banged full way through!' 'Yippiehcayyeah!!'}, ...
    'Success','help');
uiwait(successbox)
close all
EC = 0;
end

